package com.summer.api.controller;

import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.summer.core.annotations.ScopeType;
import com.summer.core.util.FacesUtil;
import com.summer.order.model.Cookbook;
import com.summer.order.model.DayFood;
import com.summer.order.service.CookbookService;
import com.summer.wxapp.util.WechatAppUtil;
@Component
@Scope(ScopeType.VIEW)
public class OrderingController {

	@Resource
	CookbookService cookbookService;
	
	private DateFormat df = new SimpleDateFormat("yyyyMMdd");
	
	public void getCurrentCookbook() {
		
		HttpServletRequest request = FacesUtil.getHttpServletRequest();
		StringBuffer sb = new StringBuffer();
		try {
			request.setCharacterEncoding("UTF-8");
//			@SuppressWarnings("unchecked")
//			Map<String, String[]> parameter = request.getParameterMap();// <String,String[]>
//			String[] weUserId = parameter.get("weUserId");
//			System.out.println(weUserId[0]);
			
			Cookbook currentCookbook = cookbookService.getLastCookbook();
			List<DayFood> dayFoods = cookbookService.getDayFoodsByCookbook(currentCookbook);
			if(currentCookbook != null) {
//				JSONObject json = JSONObject.fromObject(currentCookbook);
				sb.append("{");
				sb.append("\"data\":");
				sb.append("{");
				sb.append("\"cookbook_id\":").append("\"").append(currentCookbook.getId()).append("\"");
				sb.append(",\"startDate\":").append("\"").append(df.format(currentCookbook.getStartDate())).append("\"");
				sb.append(",\"endDate\":").append("\"").append(df.format(currentCookbook.getEndDate())).append("\"");
				sb.append(",\"status\":").append("\"").append(currentCookbook.getStatus()).append("\"");
				sb.append(",\"dayFoods\":");
				
				System.out.println(sb.toString());
				StringBuffer sb1 = new StringBuffer();
				sb1.append("{");
				
				for (int i = 0; i < dayFoods.size(); i++) {
					DayFood dayFood = dayFoods.get(i);
					dayFood.getBreakfast();
					dayFood.getDate();
					dayFood.getFood1();
					dayFood.getFood2();
					dayFood.getId();
					dayFood.getLunch();
					dayFood.getWeekday();
					if(i != 0) {
						sb1.append(",");
					}
					sb1.append("\"dayFood"+i+"\":");
					sb1.append("{");
					sb1.append("\"id\":").append("\"").append(dayFood.getId()).append("\"");
					sb1.append(",\"date\":").append("\"").append(df.format(dayFood.getDate())).append("\"");
					sb1.append(",\"food1\":");
					sb1.append("{");
					sb1.append("\"id\":").append("\"").append(dayFood.getFood1().getId()).append("\"");
					sb1.append(",\"name\":").append("\"").append(dayFood.getFood1().getName()).append("\"");
					sb1.append(",\"picture\":").append("\"").append(dayFood.getFood1().getPicture()).append("\"");
					sb1.append("}");
					sb1.append(",\"food2\":");
					sb1.append("{");
					sb1.append("\"id\":").append("\"").append(dayFood.getFood2().getId()).append("\"");
					sb1.append(",\"name\":").append("\"").append(dayFood.getFood2().getName()).append("\"");
					sb1.append(",\"picture\":").append("\"").append(dayFood.getFood2().getPicture()).append("\"");
					sb1.append("}");
					sb1.append(",\"weekday\":").append("\"").append(dayFood.getWeekday()).append("\"");
					sb1.append("}");
					
				}
				sb1.append("}");
				sb.append(sb1);
				
				sb.append("}");
				sb.append(",\"resp\":\"");
				sb.append("success\"");
				
				
				sb.append("}");
			}
			WechatAppUtil.responseMsg(sb.toString());
			
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
	}
}
